package com.ncp.servlet.registerlogin;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ncp.model.biz.registerlogin.LoginBiz;

public class InsertMyImgServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

        
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		int UserID = Integer.parseInt(request.getParameter("UserID")); 
		String imgname =  request.getParameter("imgname");
		String path1 = request.getServerName();
		String path2 = request.getRealPath(path1);
		int n=path2.lastIndexOf("\\");
		String S_realPath=path2.substring(0,n);
		String realPath =S_realPath+"\\"; 
		boolean b1 = false,b2 = false;
        
		LoginBiz mbpb= new LoginBiz();
		
		
		
		b2=mbpb.delImgsInTomcat(UserID, realPath);
	    b1=mbpb.setMyImg(UserID,"images/UserImage/"+imgname); 
	    String img = "images/UserImage/"+imgname;
	    HttpSession session = request.getSession(true);
	    session.setAttribute("UserImg",img);
		if(b1){
			String msg;
			msg = "上传头像成功！";
			out.print("<script language=javascript>alert('"+msg+"');history.go(-1)</script>");
			request.getRequestDispatcher("pages/registerlogin/modifyregister.jsp").forward(request, response);
		}
		
	}

}
